﻿using System.Collections.Generic;

using UnityEngine;

#if SINGLETON_SETTING
using SingletonSetting.Runtime;
#else
using System.IO;
using UnityEditor;
#endif

namespace ComponentAutoBind.Editor
{
    /// <summary>
    /// 组件自动绑定全局规则设置。
    /// </summary>
#if SINGLETON_SETTING
    [SettingFilePath("Assets/EditorSetting/AutoBindGlobalSetting.asset")]
#else
#endif
    public class AutoBindGlobalSetting : ScriptableObject
    {
        #region 字段
        [SerializeField]
        private string m_ScriptFolderPath;

        [SerializeField]
        private string m_Namespace;

        [SerializeField]
        private string m_CompanyName;

        [SerializeField]
        private string m_ScriptHeader;

        [SerializeField]
        private List<string> m_HelperAssemblyNames = new List<string>();

        [SerializeField]
        private List<Author> m_Authors = new List<Author>();
        #endregion

        #region 属性
        /// <summary>
        /// 脚本目录路径。
        /// </summary>
        public string ScriptFolderPath => m_ScriptFolderPath;

        /// <summary>
        /// 脚本命名空间。
        /// </summary>
        public string Namespace => m_Namespace;

        /// <summary>
        /// 脚本公司名信息。
        /// </summary>
        public string CompanyName => m_CompanyName;

        /// <summary>
        /// 脚本头部信息。
        /// </summary>
        public string ScriptHeader => m_ScriptHeader;

        /// <summary>
        /// 绑定规则辅助器查找程序集
        /// </summary>
        public List<string> HelperAssemblyNames => m_HelperAssemblyNames;

        /// <summary>
        /// 开发者列表。
        /// </summary>
        public List<Author> Authors => m_Authors;
        #endregion

#if SINGLETON_SETTING
#else
        /// <summary>
        /// 创建组件自动绑定全局规则设置。
        /// </summary>
        public static void CreateAutoBindGlobalSetting(string settingFilePath = "")
        {
            // 默认配置路径
            if (string.IsNullOrEmpty(settingFilePath))
            {
                settingFilePath = "Assets/EditorSetting/AutoBindGlobalSetting.asset";
            }

            string[] paths = AssetDatabase.FindAssets("t:AutoBindGlobalSetting");

            if (paths.Length >= 1)
            {
                string path = AssetDatabase.GUIDToAssetPath(paths[0]);
                EditorUtility.DisplayDialog("警告", $"已存在AutoBindGlobalSetting配置，路径: {path}", "确认");

                Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(path);
                EditorGUIUtility.PingObject(Selection.activeObject);

                return;
            }

            string directoryPath = Path.GetDirectoryName(settingFilePath);

            if (string.IsNullOrEmpty(directoryPath))
            {
                Debug.LogError($"保存文件路径错误: {settingFilePath}.");

                return;
            }

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            AutoBindGlobalSetting setting = CreateInstance<AutoBindGlobalSetting>();

            AssetDatabase.CreateAsset(setting, settingFilePath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
#endif
    }
}